﻿using System;
using System.Data;
using System.Text;
using System.Threading;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;


namespace AnyOffice.general.calendar.info
{
    public partial class index : ModulePage
    {
        private Thread thread;
        private CultureInfo cultureInfo;
        private string str = "";

        protected void DataCalendar1_PreRender(object sender, EventArgs e)
        {
            this.thread = Thread.CurrentThread;
            this.cultureInfo = this.thread.CurrentCulture;
            CultureInfo info = (CultureInfo)this.cultureInfo.Clone();
            info.DateTimeFormat.DayNames = new string[] { "日", "一", "二", "三", "四", "五", "六" };
            info.DateTimeFormat.FirstDayOfWeek = DayOfWeek.Sunday;
            this.thread.CurrentCulture = info;
        }
        protected void DataCalendar1_SelectionChanged(object sender, EventArgs e)
        {
            this.ViewState["NowDay"] = this.DataCalendar1.SelectedDate.ToString("yyyy-MM-dd");
            this.GetDataSource();
            this.DataCalendar1.SelectedDate = Convert.ToDateTime("0001-1-1");
        }
        protected void ddlDeptId_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.pnlDept.Visible = false;
            this.litMessage.Visible = true;
        }
        public string GetWeekDay(DateTime date)
        {
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                    return "周日";

                case DayOfWeek.Monday:
                    return "周一";

                case DayOfWeek.Tuesday:
                    return "周二";

                case DayOfWeek.Wednesday:
                    return "周三";

                case DayOfWeek.Thursday:
                    return "周四";

                case DayOfWeek.Friday:
                    return "周五";

                case DayOfWeek.Saturday:
                    return "周六";
            }
            return "";
        }
        private void LoadDept(ref DropDownList ddlParentDept, int deptId, string strTemp)
        {
            string commandText = "SELECT [DeptId],[ParentDept],[DeptName] FROM [dbo].[Department] WHERE [ParentDept]=" + deptId.ToString() + " ORDER BY [DeptNo] ASC";
            DataTable table = SqlHelper.ExecuteDataset(base.Conn, CommandType .Text , commandText).Tables[0];
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.LoadDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        }
        private void LoadData(DropDownList ddlDeptId)
        {
            DataTable table;
            ListItem item;
            ConfigManager manager = new ConfigManager();
            UserCache cache = new UserCache();
            bool flag = cache.PrivId == manager.AdministratorPrivId;
            string commandText = "SELECT [DeptId],[ParentDept],[DeptName] FROM [dbo].[Department] WHERE [DeptId]<>-1 ";
            if (flag)
            {
                commandText = commandText + " AND [ParentDept] = 0 ORDER BY [DeptNo] ASC";
                table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        ddlDeptId.Items.Add(item);
                        this.str = "";
                        this.LoadDept(ref ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.str);
                    }
                }
            }
            else
            {
                string postPriv = cache.PostPriv;
                if (postPriv != null)
                {
                    if (!(postPriv == "0"))
                    {
                        if (!(postPriv == "1"))
                        {
                            if (postPriv == "2")
                            {
                                commandText = commandText + " AND CHARINDEX(','+ CAST([DeptId] AS nvarchar(20)) + ',','," + cache.PostDept + ",') > 0 ORDER BY [DeptNo] ASC";
                                table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                                if (table.Rows.Count > 0)
                                {
                                    for (int j = 0; j < table.Rows.Count; j++)
                                    {
                                        item = new ListItem("|─" + table.Rows[j]["DeptName"].ToString(), table.Rows[j]["DeptId"].ToString());
                                        ddlDeptId.Items.Add(item);
                                    }
                                    return;
                                }
                                item = new ListItem("无可管理部门", "0");
                                ddlDeptId.Items.Add(item);
                            }
                            return;
                        }
                    }
                    else
                    {
                        commandText = commandText + " AND [DeptId] = " + cache.DeptId.ToString();
                        table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text , commandText).Tables[0];
                        if (table.Rows.Count > 0)
                        {
                            item = new ListItem("|─" + table.Rows[0]["DeptName"].ToString(), table.Rows[0]["DeptId"].ToString());
                            ddlDeptId.Items.Add(item);
                        }
                        return;
                    }
                    commandText = commandText + " AND [ParentDept] = 0 ORDER BY [DeptNo] ASC";
                    table = SqlHelper.ExecuteDataset(base.Conn, CommandType.Text, commandText).Tables[0];
                    if (table.Rows.Count > 0)
                    {
                        for (int k = 0; k < table.Rows.Count; k++)
                        {
                            item = new ListItem("|─" + table.Rows[k]["DeptName"].ToString(), table.Rows[k]["DeptId"].ToString());
                            ddlDeptId.Items.Add(item);
                            this.str = "";
                            this.LoadDept(ref ddlDeptId, int.Parse(table.Rows[k]["DeptId"].ToString()), this.str);
                        }
                    }
                }
            }
        }

        private void rptDept_ItemDataBound (object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Repeater repeater = (Repeater)e.Item.FindControl("rptCalendar");
                if (repeater != null)
                {
                    DataRowView dataItem = (DataRowView)e.Item.DataItem;
                    CalendarDao dao = new CalendarDao();
                    DataView defaultView = dao.SpecifyDay(base.Conn, dataItem.Row.ItemArray[0].ToString(), this.DataCalendar1.SelectedDate).DefaultView;
                    defaultView.RowFilter = "CalType=1";
                    DataView view3 = defaultView;
                    repeater.DataSource = view3;
                    repeater.DataBind();
                }
            }
        }
        private void GetDataSource()
        {
            this.pnlDept.Visible = true;
            this.litMessage.Visible = false;
            DataTable table = new UsersDao().SelectBaseByDeptId(base.Conn, Convert.ToInt32(this.ddlDeptId.SelectedValue));
            this.rptDept.DataSource = table;
            this.rptDept.DataBind();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.LoadData(this.ddlDeptId);
                if (this.selectYear.Items.FindByValue(DateTime.Now.Year.ToString()) != null)
                {
                    this.selectYear.Items.FindByValue(DateTime.Now.Year.ToString()).Selected = true;
                }
                if (this.selectMonth.Items.FindByValue(DateTime.Now.Month.ToString()) != null)
                {
                    this.selectMonth.Items.FindByValue(DateTime.Now.Month.ToString()).Selected = true;
                }
                this.ViewState["NowMonth"] = this.selectYear.Value + "-" + this.selectMonth.Value + "-1";
                this.ViewState["NowDay"] = DateTime.Now.ToString("yyyy-MM-dd");
                this.DataCalendar1.VisibleDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                this.pnlDept.Visible = false;
                this.litMessage.Text = base.ShowPromptNoNav("选定部门后，请点击日历上的日期进行查询");
            }
            if (this.Page.IsPostBack)
            {
                this.pnlDept.Visible = false;
                this.litMessage.Visible = true;
                this.DataCalendar1.VisibleDate = new DateTime(Convert.ToInt32(this.selectYear.Value), Convert.ToInt32(this.selectMonth.Value), 1);
                this.ViewState["NowMonth"] = this.selectYear.Value + "-" + this.selectMonth.Value + "-1";
            }
        }
        protected override void OnInit(EventArgs e)
        {
           
            this.rptDept.ItemDataBound += new RepeaterItemEventHandler(this.rptDept_ItemDataBound );
            base.OnInit(e);
        } 

    }
}
